import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler' // 使用现代Sass编译器API
      }
    }
  },
  server: {
    host: '0.0.0.0',  // 允许局域网访问
    port: 3000,
    strictPort: false,  // 如果端口被占用，尝试下一个
    cors: true,  // 启用 CORS
    // 配置 HTTP 服务器选项
    hmr: {
      overlay: false  // 禁用错误覆盖层
    },
    // 监听所有网络接口
    open: false,  // 不自动打开浏览器
    proxy: {
      '/api': {
        target: 'http://localhost:8080',  // 后端API服务器地址
        changeOrigin: true,
        secure: false,
        timeout: 10000
      },
      '/recordings': {
        target: 'http://localhost:8080',  // 录屏文件静态服务
        changeOrigin: true,
        secure: false,
        timeout: 30000
      },
      '/client-api': {
        target: 'http://127.0.0.1:8090',  // 客户端API服务器地址
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/client-api/, '/api')
      },
      '/ws': {
        target: 'ws://localhost:8080',   // WebSocket服务器地址
        ws: true,
        changeOrigin: true
      },
      // 视频流WebSocket代理
      '/osd': {
        target: 'ws://2.2.2.149:9080',   // 视频流WebSocket服务器地址
        ws: true,
        changeOrigin: true,
        secure: false
      },
      // 摄像头设备管理相关API代理
      '/videoapi': {
        target: 'http://2.2.2.149:9980',  // 视频API服务器地址
        changeOrigin: true,
        secure: false,
        timeout: 10000,
        rewrite: (path) => path.replace(/^\/videoapi/, '')
      },
      '/gbapi': {
        target: 'http://2.2.2.149:9988',  // 国标API服务器地址
        changeOrigin: true,
        secure: false,
        timeout: 10000,
        rewrite: (path) => path.replace(/^\/gbapi/, '')
      },
      '/appapi': {
        target: 'http://2.2.2.149:8081',  // 应用API服务器地址
        changeOrigin: true,
        secure: false,
        timeout: 10000,
        rewrite: (path) => path.replace(/^\/appapi/, '')
      },
      '/nodeapi': {
        target: 'http://2.2.2.149:8081',  // 节点API服务器地址
        changeOrigin: true,
        secure: false,
        timeout: 10000,
        rewrite: (path) => path.replace(/^\/nodeapi/, '')
      },
      '/data': {
        target: 'http://2.2.2.149:8081',  // 数据API服务器地址
        changeOrigin: true,
        secure: false,
        timeout: 10000,
        rewrite: (path) => path.replace(/^\/data/, '')
      }
    }
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets'
  }
})
